package net.skyedu.service.myattend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.skyedu.bean.ShowUser;
import net.skyedu.bean.SumLate;
import net.skyedu.dao.AjaxDao;

public class MonthUser extends HttpServlet
{
  private static final long serialVersionUID = -7452492747665303419L;

  public void destroy()
  {
    super.destroy();
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    response.setContentType("text/html;charset=UTF-8");
    doPost(request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");

    int USERID = Integer.parseInt(request.getParameter("USERID"));
    String code = request.getParameter("code");
    int currentpage = Integer.parseInt(request.getParameter("currentPage"));
    int pagesize = Integer.parseInt(request.getParameter("pageSize"));
    int startIndex = 0;
    try {
      startIndex = (currentpage - 1) * pagesize;
      AjaxDao ado = new AjaxDao();
      StringBuffer jsonAttr = new StringBuffer("[");
      int pageCount = 0;
      List list = ado.showMyAttend(startIndex, pagesize, 1, USERID, code);
      pageCount = ado.showMyAttend(startIndex, pagesize, 0, USERID, code).size();
      if (pageCount != 0) {
        for (int i = 0; i < list.size(); i++) {
          ShowUser su = (ShowUser)list.get(i);
          jsonAttr.append("{");
          SumLate sl = ado.getSum(USERID, code);
          jsonAttr.append("\"lateSum\":\"" + sl.getLateSum() + "\",");
          jsonAttr.append("\"earlySum\":\"" + sl.getEarlySum() + "\",");

          String checkbox = "";
          if (su.getAttconfirm() != 5)
            checkbox = "<input type='checkbox' title='不可勾选' disabled='disabled'>";
          else {
            checkbox = "<input name='items' type='checkbox' value='" + su.getAttid() + "'>";
          }
          jsonAttr.append("\"check\":\"" + checkbox + "\",");
          jsonAttr.append("\"attId\":\"" + su.getAttid() + "\",");
          jsonAttr.append("\"userId\":\"" + su.getUserId() + "\",");
          jsonAttr.append("\"userName\":\"" + su.getUsername() + "\",");
          jsonAttr.append("\"schName\":\"" + su.getDeptname() + "\",");
          String week = "";
          if (su.getAttweek() == 1) week = "<font color='red'>星期天</font>";
          if (su.getAttweek() == 2) week = "星期一";
          if (su.getAttweek() == 3) week = "星期二";
          if (su.getAttweek() == 4) week = "星期三";
          if (su.getAttweek() == 5) week = "星期四";
          if (su.getAttweek() == 6) week = "星期五";
          if (su.getAttweek() == 7) week = "<font color='red'>星期六</font>";
          jsonAttr.append("\"attWeek\":\"" + week + "\",");

          jsonAttr.append("\"attDate\":\"" + su.getAttdate() + "\",");
          String worktime = "";
          if (su.getAttworktime() != null) {
            worktime = su.getAttworktime();
          }
          String closingtime = "";
          if (su.getAttclosingtime() != null) {
            closingtime = su.getAttclosingtime();
          }
          jsonAttr.append("\"worktime\":\"" + worktime + "\",");
          jsonAttr.append("\"closingtime\":\"" + closingtime + "\",");
          String state = "";
          if (su.getAttstate() == 1) state = "<font color='green'>正常</font>";
          if (su.getAttstate() == 2) state = "<font color='#CC0000'>迟到</font>";
          if (su.getAttstate() == 3) state = "<font color='#CC0099'>早退</font>";
          if (su.getAttstate() == 4) state = "<font color='#CC0099'>调休</font>";
          if (su.getAttstate() == 5) state = "<font color='#FF0000'>旷工</font>";
          if (su.getAttstate() == 6) state = "<font color='#0000FF'>出差</font>";
          if (su.getAttstate() == 7) state = "<font color='#F72056'>婚假</font>";
          if (su.getAttstate() == 8) state = "<font color='#FF6600'>年假</font>";
          if (su.getAttstate() == 9) state = "<font color='#000000'>病假</font>";
          if (su.getAttstate() == 10) state = "<font color='#000000'>事假</font>";
          if (su.getAttstate() == 11) state = "<font color='#000000'>丧假</font>";
          if (su.getAttstate() == 12) state = "<font color='#CC0000'>产假</font>";
          if (su.getAttstate() == 13) state = "<font color='#CC0000'>陪产假</font>";
          jsonAttr.append("\"state\":\"" + state + "\",");

          if (su.getAttlatetime() != 0)
            jsonAttr.append("\"latetime\":\"" + su.getAttlatetime() + "<font color='#6666CC'>分钟</font>\",");
          else {
            jsonAttr.append("\"latetime\":\"<font color='#FF3300'>未迟到</font>\",");
          }
          if (su.getAttearlytime() != 0)
            jsonAttr.append("\"earlytime\":\"" + su.getAttearlytime() + "<font color='#6666CC'>分钟</font>\",");
          else {
            jsonAttr.append("\"earlytime\":\"<font color='#FF3300'>未早退</font>\",");
          }
          String desc = "";
          StringBuffer subDesc = new StringBuffer("");
          if ((su.getAttdesc() != null) && (!"".equals(su.getAttdesc()))) {
            desc = su.getAttdesc();
            if (desc.length() > 3)
              subDesc.append(desc.substring(0, 3)).append("..");
            else {
              subDesc.append(desc);
            }
          }
          if(desc.indexOf("\r\n")>0)
    	  {
    		  System.out.println(desc.indexOf("\r\n"));
    		  desc=desc.substring(0, desc.indexOf("\r\n"));
    	  }

          jsonAttr.append("\"subDesc\":\"" + subDesc + "\",");
          jsonAttr.append("\"desc\":\"" + desc + "\",");
          String confirm = "";
          if (su.getAttconfirm() == 0) confirm = "<font color='red'>处理中..</font>";
          if (su.getAttconfirm() == 1) confirm = "<font color='#999999'>已确认</font>";
          if (su.getAttconfirm() == 2) confirm = "<font color='#666666'>申诉中</font></a>";
          if (su.getAttconfirm() == 3) confirm = "<font color='#336600'>申诉成功</font></a>";
          if (su.getAttconfirm() == 4) confirm = "<font color='#666666'>已驳回</font></a>";
          if (su.getAttconfirm() == 5)
            confirm = "[<a href='javascript:void(0);' onClick='confim(1," + su.getAttid() + "," + currentpage + ");'><font color='green'>确认</font></a>]" + 
              "[<a href='javascript:void(0);' onclick='opens(" + su.getUserId() + "," + su.getAttid() + "," + currentpage + ");'><font color='green'>申诉</font></a>]";
          jsonAttr.append("\"confirm\":\"" + confirm + "\",");

          jsonAttr.append("\"code\":\"" + code + "\",");
          jsonAttr.append("\"pageCount\":\"" + pageCount + "\"");
          jsonAttr.append("}");
          if (i < list.size() - 1)
            jsonAttr.append(",");
        }
      }
      else {
        jsonAttr.append("{");
        jsonAttr.append("\"code\":\"" + code + "\",");
        jsonAttr.append("\"pageCount\":\"" + pageCount + "\"");
        jsonAttr.append("}");
      }
      jsonAttr.append("]");

      PrintWriter out = response.getWriter();
      out.print(jsonAttr);
      out.flush();
      out.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public void init()
    throws ServletException
  {
  }
}